package com.nxn.core.web.action.base;

import java.io.Serializable;
import java.util.Collections;
import java.util.Map;

import com.nxn.core.utils.Page;

public abstract class DWZCRUDAction<T, PK extends Serializable> extends CRUDAction<T, PK>{
	private static final long serialVersionUID = 8487835304239158330L;
	
	/**
	 * DWZ AJAX 返回数据
	 * {"statusCode":"200", 
		"message":"操作成功", 
		"navTabId":"navNewsLi", 
		"forwardUrl":"", 
		"callbackType":"closeCurrent"}
	 */
	
	protected String statusCode = "200";
	protected String message = "操作成功";
	protected String navTabId = "";
	protected String forwardUrl;
	protected String callbackType = "closeCurrent";
	
	/** Page对象 */
    protected Page<T> page = new Page<T>(20);
    
	/**
	 * 子类重写该方法，可以加入查询参数
	 * @return
	 */
	protected Map<String, Object> buildQueryMap(){
		return Collections.<String, Object>emptyMap();
	}
    
	/**
	 * Action函数, 默认的action函数, 默认调用list()函数.
	 */
	@Override
	public String execute() throws Exception {
		return page();
	}

	@Override
	public String delete() throws Exception {
		setCallbackType(null);
		setNavTabId(null);
		
		return super.delete();
	}

	// -- CRUD Action函数 --//
	/**
	 * Action函数,显示Entity列表界面. 建议return SUCCESS.
	 */
	public String page() throws Exception {
		try {
			int pageNum = getParameterForInt("pageNum");
			page.setPageNo(pageNum);

			String[] obj = entityClass.toString().split("\\.");
			String realObj = obj[obj.length - 1];

			String hql = "from " + realObj;

			crudService.queryPage(page, hql, buildQueryMap());

		} catch (Exception e) {
			logger.error("读取实体类[{}]列表失败! 错误原因: {}", this.entityClass, e.getMessage());
		}

		return SUCCESS;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}
}
